import java.util.Date;
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;


public class Bar {
	DateFormat formatterIn = new SimpleDateFormat("yyyy/MM/dd");
	Format formatterOut = new SimpleDateFormat("yyyy/MM/dd E");
    
	@SuppressWarnings("deprecation")
	public Bar(String[] data) {
		Date date = null;
		try {
			date = formatterIn.parse(data[0]);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		today = new GregorianCalendar(date.getYear() + 1900, date.getMonth(), date.getDate());
		open = Double.parseDouble(data[1]);
		high = Double.parseDouble(data[2]);
		low = Double.parseDouble(data[3]);
		close = Double.parseDouble(data[4]);
		volume = Double.parseDouble(data[5]);  // millions 
		range = high - low;
		
		year = today.get(Calendar.YEAR);
		month = today.get(Calendar.MONTH) + 1;
		day = today.get(Calendar.DAY_OF_MONTH);
		weekDay = (today.get(Calendar.DAY_OF_WEEK) + 6) % 7;
		dateString = formatterOut.format(today.getTime());
	}
	
	@Override
	public String toString() {
		return String.format("%s %d, 開=%.1f, 高=%.1f, 低=%.1f, 收 =%.1f, Range=%.1f, 漲跌=%.1f", dateString, weekDay, open, high, low, close, range, diff);
	}

  public String mergeBar(Bar bar2) {
    return String.format("%s %d,開=%.1f[%.0f],高=%.1f[%.0f],低=%.1f[%.0f],收 =%.1f[%.0f],Range=%.1f[%.0f],漲跌=%.1f[%.0f],價差=%.1f", 
        dateString, weekDay, open, bar2.open, high, bar2.high, low, bar2.low, close, bar2.close, range, bar2.range, diff, bar2.diff, bar2.close - close);
  }

	public double high;
	public double low;
	public double open;
	public double close;
	public double volume;
	public double diff = 0;
	public double range;
	public Calendar today;
	
	public int year;
	public int month;
	public int day;
	public int weekDay;  // 1~6 = Mon~Sat, 7 = Sun
	public String dateString;
}
